// pages/my/my.js
var Login = require("../../Controls/Login")
var Protocol = require("../../Controls/Protocol")
const config = require("../../config")

var app = getApp();

var menus_release = 
[
  
]

var menus_dev = 
[
  {
    name: "常见问题",
    img: "../../image/my/icon_faq.png",
    page_url: "../../pages/index/index",
  },
  {
    name: "联系客服",
    img: "../../image/my/icon_service.png",
    page_url: "../../pages/index/index",
  },
  {
    name: "测试接口",
    img: "../../image/my/icon_faq.png",
    page_url: "../../pages/test/test",
  }
]

var operation = 
{
  data : 
  {
    isLogin:false,

    balance:"0.000",

    userInfo:
    {
      avatarUrl: "../../image/yellowchild.jpg",
      nickName: "",
    },

    data_menus:
    [
      { value: 0, name: "参与秘袋", onClick: "btnMyJoin"},
      { value: 0, name: "发布秘袋", onClick: "btnMyPublish"},
      { value: 0, name: "获得秘袋", onClick: "btnMyGot"},
    ],

    menus: config.isProduction?menus_release:menus_dev,
  }
}

function btnInfoIvk()
{
  wx.navigateTo({
    url: '../myinfo/myinfo',
  })
}

function btnMyJoin()
{
  wx.navigateTo({ url: "../myattand/myattand"})
}

function btnMyPublish() 
{
  wx.navigateTo({ url: "../mypublished/mypublished"});
}

function btnMyGot() 
{
  wx.navigateTo({ url: "../mygot/mygot" });
}

function btnGetUserInfoIvk(res)
{
  Login.login(result =>{
    this.setData({ userInfo: app.globalData.userInfo, isLogin: app.globalData.isLogin, balance:app.globalData.credit.balance });
  });
}

function onShow()
{
  var that = this;

  if (app.globalData.isLogin) {
    this.setData({ userInfo: app.globalData.userInfo, isLogin: app.globalData.isLogin, balance: app.globalData.credit.balance });
    initDataMenu(this);
    return;
  }
}

function onLoad() {
  var that = this;

  wx.getSetting({
    success(res)
    {
      if (res.authSetting["scope.userInfo"]) {
        Login.login(result => {
          that.setData({ userInfo: app.globalData.userInfo, isLogin: app.globalData.isLogin, balance: app.globalData.credit.balance });
          initDataMenu(that);
        });
      }
    }
  })  
}

function onReady()
{
  this.Tip = this.selectComponent("#Tip");
  this.Popup = this.selectComponent("#Popup");

  var that = this;
  wx.getSetting({
    success(res)
    {
      if(!res.authSetting["scope.userInfo"])
      {
        //that.Popup.show()
        wx.reLaunch({
          url: '../login/login?frompath=../my/my&istabBar=true',
        })
      }
    },
    fail()
    {
      wx.reLaunch({
        url: '../login/login?frompath=../my/my&istabBar=true',
      })
      //that.Popup.show()
    }

  })
  
}

function showTip()
{
  this.Tip.showDialog();
}

function initDataMenu(that)
{
  Protocol.myInfo({
    success:function(res)
    {
      console.log(res);
      var menu = that.data.data_menus;
      for(var i = 0; i < menu.length; i++)
      {
        if (menu[i].name == "参与秘袋")
        {
          menu[i].value = res.data.data.attanded;
        }
        else if (menu[i].name == "发布秘袋")
        {
          menu[i].value = res.data.data.published;
        }
        else if (menu[i].name == "获得秘袋")
        {
          menu[i].value = res.data.data.acquired;
        }
      }

      that.setData({data_menus:menu});
    },
    fail:function(res)
    {
      console.error(res);
    }
  })
}

//自定义分享内容
function onShareAppMessage(options) {
}

operation.onLoad = onLoad;
operation.btnInfoIvk = btnInfoIvk;
operation.btnMyJoin = btnMyJoin;
operation.btnMyPublish = btnMyPublish;
operation.btnMyGot = btnMyGot;
operation.btnGetUserInfoIvk = btnGetUserInfoIvk;
operation.onReady = onReady;
operation.showTip = showTip;
operation.onShareAppMessage;
operation.onShow = onShow;

Page(operation)